/*
 * 本类作用：工具类
 * 
 * 作者：袁小杰
 * 博客：http://blog.csdn.net/undoner
 * GIT：https://git.oschina.net/undoner
 * QQ：15137281
 * 
 */
package org.mina.util;

import java.util.Properties;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class ConfigProperties {
	private Properties pro;
	private FileInputStream inputFile;
	private FileOutputStream outputFile;

	public ConfigProperties() {
		pro = new Properties();
	}

	public ConfigProperties(String filePath) {
		pro = new Properties();
		try {
			InputStream in=new FileInputStream(filePath);
            BufferedReader bf=new BufferedReader(new InputStreamReader(in,"UTF-8"));
			pro.load(bf);
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public String getValue(String key) {
		if (pro.containsKey(key)) {
			String value = pro.getProperty(key);
			return value;
		} else
			return " ";

	}

	public String getValue(String filePath, String key) {
		String value = "";
		try {
			inputFile = new FileInputStream(filePath);
			pro.load(inputFile);
			inputFile.close();
			if (pro.containsKey(key)) {
				value = pro.getProperty(key);
				return value;
			} else
				return value;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return " ";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return " ";
		}

	}

	public void clear() {
		pro.clear();
	}

	public void setValue(String value, String key) {
		pro.setProperty(key, value);
	}

	public void saveFile(String filePath, String desc)
			throws FileNotFoundException {
		outputFile = new FileOutputStream(filePath);
		try {
			pro.store(outputFile, desc);
			outputFile.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		try {  //返回当前绝对路径
			System.out.println(new File(".").getCanonicalPath());
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		String myFilePath= System.getProperty("user.dir")+"\\FrameConfig.properties";
		ConfigProperties pc = new ConfigProperties(myFilePath);
		System.out.println(pc.getValue("LocalPort").trim().equals(""));
		String name = pc.getValue("LocalPort");
		System.out.println("LocalPort: " + name );
//		ConfigProperties pc1 = new ConfigProperties(myFilePath);
//		pc1.clear();
//		String name1 = pc1.getValue("username");
//		String pwd1 = pc1.getValue("password");
//		System.out.println("Account: " + name1 + "\n" + "password: " + pwd1); 
//		pc1.setValue("test20@mailinator.com","email");
//		pc1.setValue("test20", "passwd");
//		try {
//			pc1.saveFile(".\\src\\main\\java\\config\\parameter.properties", "test");
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		name1 = pc1.getValue("email");
//		pwd1 = pc1.getValue("passwd");
//		System.out.println("Account: " + name1 + "\n" + "password: " + pwd1);

	}
}